import java.util.Scanner;

import static java.awt.AWTEventMulticaster.add;

public class ShopCarTest {
    public static void main(String[] args) {
        //购物车对象使用数组对象
        Goods[] shopCar=new Goods[100];
        //搭建操作架构,添加修改，查看
        while (true) {
            System.out.println("请您选择如下的命令进行操作");
            System.out.println("添加商品到购物车：add");
            System.out.println("查询商品到购物车：query");
            System.out.println("修改商品购买数量：update");
            System.out.println("结算购买商品金额：pay");
            Scanner sc=new Scanner(System.in);
            System.out.println("请您输入命令");
            String command=sc.next();
            switch(command){
                case "add":
                    addGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar,sc);
                    break;
                case "pay":
                    pay(shopCar);
                    break;
                default:
                    System.out.println("没有命令查询");
            }
        }
    }

    private static void pay(Goods[] shopCar) {
        queryGoods(shopCar);
        //定义一个求和变量累加金额
        double money=0;
        //遍历购物车所有商品
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if(g!=null){
                money+=g.price*g.buynumber;
            }else{
                break;
            }
        }
        System.out.println("订单价格："+money);
    }

    private static void updateGoods(Goods[] shopCar,Scanner sc ) {
        //根据用户输入要求修改商品id，根据id查出要修改的对象
        while (true) {
            System.out.println("请您输入要修改的商品id");
            int id =sc.nextInt();
            Goods g= getGoodsById(shopCar,id);
            if(g==null){
                //没有商品
                System.out.println("对不起，没有购买商品");
            }else{
                System.out.println("请您输入"+g.name+"商品最新购买数量：");
                int buynumber=sc.nextInt();
                g.buynumber=buynumber;
                System.out.println("修改完成");
                queryGoods(shopCar);
                break;
            }
        }
    }
    public static Goods getGoodsById(Goods[] shopCar,int id){
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if(g!=null){
                //判断商品id是否我们自己需要找的
                if(g.id==id){
                    return g;
                }else{
                    return null;
                }
            }
        }
        return null;//找完了100个商品，没有找到id一样的
    }

    private static void queryGoods(Goods[] shopCar) {
    //查询购物车中的商品对象信息，并展示出来
        System.out.println("========查询购物车信息如下=======");
        System.out.println("编号\t\t名称\t\t\t价格\t\t\t购买数量");
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if (g != null) {
                //展示这个商品对象
                System.out.println(g.id + "\t\t" + g.name + "\t\t\t" + g.price + "\t\t\t" + g.buynumber);

            } else {
                //遍历结束
                break;

            }
        }
    }

    private static void addGoods(Goods[] shopCar,Scanner sc) {
//录入用户输入商品信息：
        System.out.println("请您输入购买商品编号");
        int id=sc.nextInt();
        System.out.println("请您输入购买商品的名称");
        String name=sc.next();
        System.out.println("请您输入商品数量");
        int buynumber=sc.nextInt();
        System.out.println("请您输入购买商品的价格");
        double price =sc.nextDouble();
        //2把购买商品信息封装成一个对象
        Goods g=new Goods();
        g.id=id;
        g.name=name;
        g.buynumber=buynumber;
        g.price=price;
        //商品对象到购物车数组里面去
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]==null){
                shopCar[i]=g;
                break;
            }
        }
        System.out.println("您的商品："+g.name+"添加购物车完成");

    }
}
